#include "checkUpdateHandler.h"
#include <iostream>
#include <string.h>
#include "JsonUtil.h"
#include "ConfigUtil.h"
#include <pthread.h>

using namespace std;

checkUpdateHandler::checkUpdateHandler(/* args */)
{
}

checkUpdateHandler::~checkUpdateHandler()
{
}

void checkUpdateHandler::handler(TcpSocket *socket,map<string,string> paramMap)
{
    string clientVersion=paramMap["version"];
    //读取服务端配置文件
    map<string,string> serverMap=ConfigUtil::paraConfig("./config/updata.ini","config");
    string serverVersion=serverMap["version"];
    string preVersion=serverMap["preVersion"];

    map<string,string> resMap;
    resMap["code"]="728";
    resMap["success"]="1";
    resMap["message"]="成功";
    
    string isUpdata="1";

    //更新校验
    if (clientVersion.compare(serverVersion)==0)
    {
        isUpdata="0";
        resMap["message"]="已是最新版本";
    }else if (clientVersion.compare(preVersion)!=0)
    {
        isUpdata="0";
        resMap["message"]="当前版本过老，请重新下载";
    }
    serverMap["isUpdata"]=isUpdata;
    string data=JsonUtil::mapParseStr(serverMap);

    resMap["data"]=data;
    
    string str=JsonUtil::mapParseStr(resMap);

    Message message;
    message.type=0;//文本
    message.content=(char*)str.c_str();
    message.length=strlen(message.content);

    socket->sendMag(&message,3000);
}

checkUpdateHandler *checkUpdateHandler::getInstance()
{
    if (instance==nullptr)
    {
        instance=new checkUpdateHandler;
    }
    
    return instance;
}

checkUpdateHandler* checkUpdateHandler::instance=nullptr; 